define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clinic/index' + location.search,
                    edit_url: 'clinic/edit',
                    del_url: 'clinic/del',
                    multi_url: 'clinic/multi',
                    import_url: 'clinic/import',
                    table: 'clinic',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'id'},
                        {field: 'clinicname', title: __('Clinicname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'week', title: '营业日', operate: 'LIKE', formatter: function(value, row, index) {
                            if (value && value.indexOf(',') !== -1) {
                                var weeks = value.split(',');
                                var weekNames = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
                                var result = [];
                                weeks.forEach(function(week) {
                                    if (week >= 1 && week <= 7) {
                                        result.push(weekNames[week - 1]);
                                    }
                                });
                                return result.join(', ');
                            }
                            return value;
                        }},
                        {field: 'time', title: '预约时间', operate: 'LIKE', formatter: function(value, row, index) {
                            if (value && value.indexOf(',') !== -1) {
                                return value.split(',').join(', ');
                            }
                            return value;
                        }},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'opentime', title: __('Opentime'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

                edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
